package com.lanou3g.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanou3g.bean.Compony;
import com.lanou3g.service.ComponyService;
import com.lanou3g.util.Result;

@Controller
@RequestMapping("/compony")
public class ComponyController {
	
	@Autowired
	private ComponyService componyService;
	
	//进入公司列表页面
	@RequestMapping("/componyList")
	public String componyList() {
		return "compony/componyList";
	}

	//获取公司列表
	@ResponseBody
	@RequestMapping("/getComponyList")
	public Result getComponyList(Integer page, Integer limit) {
		return componyService.getComponies(page, limit);
	}
	
	//进入添加公司页面
	@RequestMapping("/addCompony")
	public String addCompony() {
		return "compony/addCompony";
	}
	
	//根据公司名检查公司是否存在
	@ResponseBody
	@RequestMapping("/checkComponyName/{componyName}")
	public Result checkComponyName(@PathVariable("componyName") String componyName) {
		return componyService.getComponyByComponyName(componyName);
	}
	
	//插入公司
	@ResponseBody
	@RequestMapping("/insCompony")
	public Result insCompony(Compony compony) {
		return componyService.insCompony(compony);
	}
	
	
	//进入公司编辑页面
	@RequestMapping("/editCompony/{id}")
	public String editCompony(@PathVariable("id")Integer id, HttpSession session) {
		Compony compony = componyService.findComponyById(id);
		session.setAttribute("compony", compony);
		return "compony/editCompony";
	}
	
	//更新公司信息
	@ResponseBody
	@RequestMapping("/updCompony")
	public Result updCompony(Compony compony) {
		System.out.println("要更新的compony是:" + compony);
		return componyService.updateCompony(compony);
	}
	
	//删除指定id的公司
	@ResponseBody
	@RequestMapping("/delCompony/{id}")
	public Result delCompony(@PathVariable("id")Integer id) {
		return componyService.deleteComponyById(id);
	}
	
}
